'use strict';

angular.module('hanxinBroker').controller('SubscribeCtrl', function (ngDialog, $http, $state, Auth,SubscribeService,growl) {
  var vm = this;
    vm.form = {};
     vm.addSpecialWarnMessage = function() {
       growl.addWarnMessage("预约成功",{ttl:"2000"});
     };

      vm.subscribe = function subscribe(subscribeForm) {
        SubscribeService.mobile = vm.form.telephone;

        console.log(subscribeForm.userName.$invalid);

        if(subscribeForm.$invalid){
        return;
      }

      var validatorRequest = {
        method: 'POST',
        url: 'broker/api/subscribesValidator',
        data: {"name": vm.form.userName, "mobile": vm.form.telephone}
      };

      var subscribeRequest = {
        method: 'POST',
        url: 'broker/api/subscribes',
        data: {"name": vm.form.userName, "mobile": vm.form.telephone}
      };

      $http(validatorRequest).success(function (data) {
        console.log("成功" + data);
        ngDialog.openConfirm({
          template: 'subsribesuccessId',
          className: 'ngdialog-theme-default subscribe-dialog',
          controller: 'SaveSuccessCtrl'
        }).then(function (value) {
          console.log('Send data to saving database: ', value);
          $http(subscribeRequest).success(function(data){
            console.log("save successful.."+data);
            vm.form={};
            subscribeForm.$setPristine();
            vm.addSpecialWarnMessage();
          }).error(
            console.log("save failed..."+data)
          );

        }, function (reason) {
          console.log('cancel save', reason);
        });

      }).error(function (data) {
        if (data.message == 'DUPLICATED_MOBILE') {
          ngDialog.openConfirm({
            template: 'modalDialogId',
            className: 'ngdialog-theme-default subscribe-dialog',
            controller:'SaveDuplicatedCtrl'
          }).then(function (value) {
            vm.form = {};
            subscribeForm.$setPristine();
            console.log('DUPLICATED_MOBILE,clean this form', value);
          }, function (reason) {
            console.log('cancel this option', reason);
          });
        }

        if (data.message == 'REGISTERED') {
          ngDialog.openConfirm({
            template: 'duplicatedMobileId',
            className: 'ngdialog-theme-default subscribe-dialog'
          }).then(function (value) {
            console.log('REGISTERED clean this form', value);
            vm.form = {};
            subscribeForm.$setPristine();
          }, function (reason) {
            console.log('cancel this option', reason);
          });
        }
      });
    };
}).controller("SaveDuplicatedCtrl",function(SubscribeService,$scope,$http){
    $scope.phoneNum = SubscribeService.mobile;
    $scope.validity = "";
    var validityReq = {
      method : 'POST',
      url :  'broker/api/subscribeFindByMobile',
      data:  {"name":"","mobile": $scope.phoneNum}
    };

    $http(validityReq).success(function(data){
       $scope.validity = data.validity;
    });
}).controller("SaveSuccessCtrl",function($scope,SubscribeService){
    $scope.mobile = SubscribeService.mobile;
});
